package com.wanger.swc.exception.handle;

import com.wanger.swc.exception.BusEx;
import com.wanger.swc.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: wanger
 * @Date: 2024/9/30 1:15
 * @Description: 全局异常处理器
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理方法
     */
    @ResponseBody
    @ExceptionHandler(Throwable.class)
    public Result<String> exceptionHandler(Throwable e) {
        if (e instanceof BusEx) {
            log.warn("", e);
            return Result.error(500, ((BusEx) e).getMsg());
        }
        //默认情况！
        log.error("", e);
//        return Result.error500();
        return Result.error(500, "服务器异常！" + e.getMessage());
    }
}
